package com.zsr.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.config.annotation.RequiresPermissions;
import com.zsr.example.config.exception.CommonJsonException;
import com.zsr.example.entity.EduPath;
import com.zsr.example.entity.EduPathCategory;
import com.zsr.example.service.EduPathCategoryService;
import com.zsr.example.service.EduPathService;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.vo.PathReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学习路径 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/path")
public class EduPathController {

    @Autowired
    private EduPathService pathService;

    @Autowired
    private EduPathCategoryService pathCategoryService;

    @RequiresPermissions("path:list")
    @GetMapping("/getCategoryList")
    public JSONObject getCategoryList() {
        List<EduPathCategory> list = pathCategoryService.getListOrderBy();
        return CommonUtil.successJson(list);
    }

    @RequiresPermissions("path:update")
    @PostMapping("/saveOrUpdateCategory")
    public JSONObject saveOrUpdateCategory(@RequestBody EduPathCategory category) {
        pathCategoryService.saveOrUpdate(category);
        return CommonUtil.successJson();
    }

    @RequiresPermissions("path:delete")
    @GetMapping("/delCategory/{id}")
    public JSONObject delCategory(@PathVariable String id) {
        EduPath path = pathService.getOne(new LambdaQueryWrapper<EduPath>()
                .eq(EduPath::getCategoryId, id));
        if (path != null) throw new CommonJsonException("20001", "该分类下有学习路径，请先删除");
        pathCategoryService.removeById(id);
        return CommonUtil.successJson();
    }

    //分割线

    /**
     * 查询学习路径列表
     */
    @RequiresPermissions("path:list")
    @PostMapping("/pageListPathCondition/{current}/{limit}")
    public JSONObject listArticle(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) PathReq query) {
        Page<EduPath> page = new Page<>(current, limit);

        pathService.pageQuery(page, query);

        return CommonUtil.successPage(page.getTotal(), page.getRecords());
    }

    @RequiresPermissions("path:list")
    @GetMapping("/getPath/{id}")
    public JSONObject getPath(@PathVariable String id) {
        EduPath path = pathService.getById(id);
        return CommonUtil.successJson(path);
    }

    @RequiresPermissions("path:delete")
    @GetMapping("/delPath/{id}")
    public JSONObject delPath(@PathVariable String id) {
        EduPath path = pathService.getById(id);
        if (path.getStepsCount() > 0) throw new CommonJsonException("20001", "该路径下有步骤，请先删除");
        pathService.removeById(id);
        return CommonUtil.successJson();
    }

    @RequiresPermissions("path:update")
    @PostMapping("/saveOrUpdatePath")
    public JSONObject saveOrUpdateCategory(@RequestBody EduPath path) {
        if (StringUtils.isEmpty(path.getId())) {
            //初始化
            path.setCoursesCount(0);
            path.setStepsCount(0);
        }
        pathService.saveOrUpdate(path);
        return CommonUtil.successJson();
    }


}

